//获取应用实例
const app = getApp();

Page({
    data:{
        now_page: 0,
        list_total: 0,
        color_list: [],
        sel_list: [],
        keywords: '',
        limit: 20,
    },
    onLoad: function () {
        // 设置标题
        wx.setNavigationBarTitle({
            title: '选择颜色'
        });
        // 接收传递过来的数据
        var __this = this;
        var eventChannel = this.getOpenerEventChannel();
        // 监听 acceptDataFromOpenerPage 事件，获取上一页面通过 eventChannel 传送到当前页面的数据
        eventChannel.on('acceptDataFromOpenerPage', function (data) {
            console.log('传过来',data)
            __this.setData({
                sel_list: data.sel_list,
            })
            // 获取颜色
            __this.get_color_list();
        });
    },

    // 页面触底时执行
    onReachBottom: function () {
        this.get_color_list();
    },
    chooseInsert:function(){

      if(this.data.keywords===undefined || this.data.keywords==''){
        wx.showToast({
          title: '请输入颜色再新增',
          icon: 'none',
          duration: 1500,
        });
        return;
      }
      var result = app.request_api('admin/MaterialColor/add', {
        name: this.data.keywords,
      }, true, 'POST');
      result.then(res => {
        console.log('res',res)
        this.get_color_list(1)
      })
    },
    // 获取颜色
    get_color_list: function (is_search = 0) {
        var __this = this;
        // 验证
        if (is_search == 1) {
          this.setData({
              color_list: [],
              list_total: 0,
              now_page: 0
          })
      }
        if ((this.data.color_list.length >= this.data.list_total) && this.data.now_page > 0) {
          console.log('暂停')
            return false;
        }
        // 发起请求
        var result = app.request_api('admin/MaterialColor/index', {
            page: this.data.now_page + 1,
            initKey: 'color.name',
            select: true,
            quick_search: __this.data.keywords,
            limit: __this.data.limit
        }, true, 'GET');
        result.then(res => {
            console.log(res)
        
            if (res.list.length > 0) {
                res.list.forEach(function (item, index) {
                    res.list[index].color = res.list[index].name;
                    res.list[index].is_checked = 0;
                    res.list[index].num = '';
                    console.log('__this.data.sel_list',__this.data.sel_list)
                    if ((__this.data.sel_list.findIndex(obj => obj.color === res.list[index].color)) > -1) {
                        res.list[index].is_checked = 1;
                    }
                });
            }
     
            this.setData({
                color_list: this.data.color_list.concat(res.list),
                now_page: this.data.now_page + 1,
                list_total: res.total,
            });
        });
    },

    // 选择颜色时
    colorChange: function (e) {
        var index = e.currentTarget.dataset.index;
        var choose_index = this.data.sel_list.findIndex(item => item.color === this.data.color_list[index].color);
        // var choose_item = this.data.sel_list.find(item => item.id === this.data.color_list[index].id);
        console.log(choose_index)
        if (e.detail.value.length) {
            if (choose_index >= 0) {
                wx.showToast({
                    title: '已经选择过此颜色了',
                    icon: 'none',
                    duration: 1500,
                });
                return false;
            }
            // 选中
            // console.log()
            let newd = {}
            newd.color = this.data.color_list[index].color
            this.data.sel_list.push(newd);
            this.data.color_list[index].is_checked = 1;
        } else {
            // 取消选中
            if (choose_index >= 0) {
                this.data.sel_list.splice(choose_index, 1);
                this.data.color_list[index].is_checked = 0;
            }
        }
        this.setData({
            sel_list: this.data.sel_list,
            color_list: this.data.color_list
        });
    },

    // 搜索
    GoSearch: function (event) {
        var keywords = event.detail.value.trim();
        this.setData({
            keywords: keywords,
            color_list: [],
            now_page: 0,
            list_total: 0,
        });
        this.get_color_list(1);
    },

    // 取消选择
    chooseDel: function (e) {
        var index = e.currentTarget.dataset.index;
        // 将此条数据删除并将对应的取消选择
        var choose_index = this.data.color_list.findIndex(obj => obj.color === this.data.sel_list[index].color);
        if (choose_index > -1) {
            this.data.color_list[choose_index].is_checked = 0;
        }
        this.data.sel_list.splice(index, 1);
        // 重置data
        this.setData({
            sel_list: this.data.sel_list,
            color_list: this.data.color_list
        });
    },

    // 当点击确定选择时
    chooseConfirm: function (e) {
        var __this = this;
        // 向上一级页面传递参数
        var eventChannel = __this.getOpenerEventChannel();
        eventChannel.emit('changeData', __this.data.sel_list);
        // 返回上一页
        wx.navigateBack();
    },

    // 输入生产数量时
    selInputChange: function (e) {
        var index = e.currentTarget.dataset.index;
        var field = e.currentTarget.dataset.field;
        console.log('ve',e,field)
        this.data.sel_list[index][field] = e.detail.value;
        this.setData({
            sel_list: this.data.sel_list
        })
    },


})
